{
 "metadata": {
  "language": "Julia",
  "name": "",
  "signature": "sha256:665084e666f49e50f619965ac4f3c8125f74dd349835443d8f6443aea15883db"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "How many cores am I using?"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nprocs()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "addprocs(2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nprocs()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "`@spawn` macro creates process on available node and runs whatever code you send it.  `fetch()` will retrieve the results -- pausing the code if it isn't ready yet"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "A = rand(1000,1000)\n",
      "Bref = @spawn A^2\n",
      "fetch(Bref)\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "A = rand(1000,1000)\n",
      "Bref = @spawn A^2\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Fetching takes time (like any memory copy operation).  But it's also slower because we create A locally, then send it via `@spawn` to be calculated.  Instead:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "Bref = @spawn rand(1000,1000)^2\n",
      "fetch(Bref)\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "Bref = @spawn rand(1000,1000)^2\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Functions, by default, are only available to the first local process"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "function count_heads(n)\n",
      "    c = 0\n",
      "    for i=1:n\n",
      "        c += randbool()\n",
      "    end\n",
      "    c\n",
      "end"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "a = @spawn count_heads(10000000)\n",
      "b = @spawn count_heads(10000000)\n",
      "total = fetch(a)+fetch(b)\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "addprocs(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nprocs()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Make function available to all procs"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "@everywhere function count_heads(n)\n",
      "    c = 0\n",
      "    for i=1:n\n",
      "        c += randbool()\n",
      "    end\n",
      "    c\n",
      "end"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "a = @spawn count_heads(1000000000)\n",
      "b = @spawn count_heads(1000000000)\n",
      "total = fetch(a)+fetch(b)\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "a2 = count_heads(1000000000)\n",
      "b2 = count_heads(1000000000)\n",
      "total = a2+b2\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "@parallel for i=1:4\n",
      "       run(`hostname`)\n",
      "end"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "nheads = @parallel (+) for i=1:1000000000\n",
      "  int(randbool())\n",
      "end\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "addprocs(2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nprocs()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tic()\n",
      "nheads = @parallel (+) for i=1:1000000000\n",
      "  int(randbool())\n",
      "end\n",
      "t = toc()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}